import { doImport } from "../api/public";
export default {
	name: "batchImport",
	data() {
		return {
			// hxf-0916-步骤条默认选中
			active: 0,
			// hxf-0916-tab默认选中
			activeName: "first",
			// hxf-0916-table数据存放
			batchImportTableData: {
				successfulData: [],
				errorData: [],
				errorSize: 0,
				successfulSize: 0
			},
			submitData: "",
			Smile: require("@/assets/images/Smile-icon.png"),
			// hxf-0916-文件上传
			publicServiceFacilityFile: "",
			url: "/api/recentdemand/batch-import"
		};
	},
	methods: {
		// hxf-0916-上一步
		handleClickPrev() {
			this.$confirm("返回上一步数据将会丢失, 是否继续?", "提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			})
				.then(() => {
					if (this.active-- < 0) this.active = 0;
				})
				.catch(() => {
					this.$message({
						type: "info",
						message: "已取消上一步"
					});
				});
		},
		// hxf-0916-下一步
		handleClicknext() {
			let formData = new FormData();
			formData.append("file", this.publicServiceFacilityFile);
			if (this.active === 0) {
				if (this.publicServiceFacilityFile) {
					this.http.post(this.url, formData).then(res => {
						if (res.data.code === 0) {
							this.batchImportTableData = res.data.data;
							this.active++;
						} else {
							this.$message.error("文件上传失败！");
							return;
						}
					});
				} else {
					this.$message.error("请先上传文件！");
				}
			} else if (this.active === 1) {
				doImport().then(res => {
					if (res.data.code === 0) {
						this.submitData = res.data.data;
						this.active++;
					} else {
						this.$message.error("操作失败！");
						return;
					}
				});
			} else {
				this.active++;
			}
		},
		// hxf-0916-完成
		handleClickSubmit() {
			//关闭当前新增页，暂时没找到好办法
			this.$store.commit("delHistoryRoute", this.$route.fullPath);
			// this.$parent.$children[2].closeTab('current')
			//跳转
			this.$router.push(
				"/planning-equipment-management/public-service-facilities/index"
			);
		},
		// hxf-0916-tab选中事件
		handleTabClick(tab, event) {
			// console.log(tab, event);
		},
		//hxf-0916-文件移除
		handleRemove(file, fileList) {
			console.log(file, fileList);
		},
		// hxf-0916-下载模板
		handleDownTemplate() {
			const { origin, pathname } = window.location;
			window.location.href = `${origin}/${
				pathname.split("/")[1]
			}/publicServiceFacilityImportTemplate.xls`;
		},
		// hxf-0916-文件上传前的钩子
		handleChange(file) {
			this.publicServiceFacilityFile = file.raw;
		},
		// hxf-0916-导出异常数据
		handleDownError() {
			this.publicExport("异常数据", "/api/recentdemand/downError", this.filter);
		}
	}
};
